package export.bullet
{
	import base.*;
	
	import com.dusk.tool.HitTest;
	
	import flash.geom.Point;
	
	public class BounceBullet extends BaseBullet
	{
		
		internal const simulatePerFrame:int = 10;
		
		public function BounceBullet(param1:String, param2:Point)
		{
			super(param1);
			this.velocity = param2;
		}
		
		internal var needToMovePoint:Point;
		internal var divideSpeed:Point;
		internal var simulateCount:int;
		internal var count:int = 0;
		internal var nextVector:Point;
		
		override protected function step():void
		{
			super.step();
			this.divideSpeed = new Point(this.velocity.x / Number(this.simulatePerFrame), this.velocity.y / Number(this.simulatePerFrame));
			this.simulateCount = 0;
			this.checkColipse();
			this.count++;
		}
		
		public function setSpeedValue(param1:int, param2:int):void
		{
			this.velocity.x = param1;
			this.velocity.y = param2;
		}
		
		internal function checkColipse():void
		{
			var _loc4_:* = undefined;
			var _loc5_:* = undefined;
			var _loc1_:* = null;
			if (!this.clip)
			{
				return;
			}
			var _loc2_:* = gc.pWorld.getTerrainArray();
			var _loc3_:* = 0;
			while (_loc3_ < _loc2_.length)
			{
				_loc1_ = _loc2_[_loc3_];
				if (HitTest.complexHitTestObject(_loc1_, this.clip))
				{
					this.nextVector = this.getNextSimulateModuleVector(_loc1_);
					this.move(this.nextVector);
					this.divideSpeed = this.nextVector;
					if (this.simulateCount < 9)
					{
						this.simulateCount++;
						this.checkColipse();
					}
					else
					{
						this.velocity = new Point(Number(this.divideSpeed.x) * Number(this.simulatePerFrame), Number(this.divideSpeed.y) * Number(this.simulatePerFrame));
					}
				}
				else
				{
					_loc1_.x += _loc1_.speedX;
					_loc1_.y += _loc1_.speedY;
					this.x += this.velocity.x;
					this.y += this.velocity.y;
					if (HitTest.complexHitTestObject(_loc1_, this.clip))
					{
						_loc1_.x -= _loc1_.speedX;
						_loc1_.y -= _loc1_.speedY;
						this.x -= this.velocity.x;
						this.y -= this.velocity.y;
						this.nextVector = this.getNextSimulateModuleVector(_loc1_);
						this.move(this.nextVector);
						this.divideSpeed = this.nextVector;
						if (this.simulateCount < 9)
						{
							this.simulateCount++;
							this.checkColipse();
						}
						else
						{
							this.velocity = new Point(Number(this.divideSpeed.x) * Number(this.simulatePerFrame), Number(this.divideSpeed.y) * Number(this.simulatePerFrame));
						}
					}
					else
					{
						_loc1_.x -= _loc1_.speedX;
						_loc1_.y -= _loc1_.speedY;
						this.x -= this.velocity.x;
						this.y -= this.velocity.y;
						if (this.simulateCount != 0)
						{
							if (this.simulateCount < 9)
							{
								this.move(this.divideSpeed);
								this.simulateCount++;
								this.checkColipse();
							}
							else
							{
								this.velocity = new Point(Number(this.divideSpeed.x) * Number(this.simulatePerFrame), Number(this.divideSpeed.y) * Number(this.simulatePerFrame));
							}
						}
					}
				}
				_loc3_++;
			}
			if (this.simulateCount == 0)
			{
				this.move(this.velocity);
			}
		}
		
		internal function getNextSimulateModuleVector(param1:BaseTerrain):Point
		{
			var _loc2_:* = param1.getMirrorVector();
			var _loc3_:* = new Point(this.divideSpeed.x, this.divideSpeed.y);
			var _loc4_:* = Math.sqrt(Number(_loc2_.x) * Number(_loc2_.x) + Number(_loc2_.y) * Number(_loc2_.y));
			var _loc5_:* = -Number(_loc2_.y) * this.velocity.x / _loc4_ + Number(_loc2_.x) * this.velocity.y / _loc4_;
			var _loc6_:* = this.getSpeedValue();
			var _loc7_:* = this.velocity.x + 2 * Number(_loc2_.y) * _loc5_ / _loc4_;
			var _loc8_:* = this.velocity.y - 2 * Number(_loc2_.x) * _loc5_ / _loc4_;
			var _loc9_:* = Math.sqrt(_loc7_ * _loc7_ + _loc8_ * _loc8_);
			_loc7_ = _loc7_ * _loc6_ / _loc9_;
			_loc8_ = _loc8_ * _loc6_ / _loc9_;
			return new Point(_loc7_, _loc8_);
		}
		
		internal function getSpeedValue():Number
		{
			return Math.sqrt(Number(this.divideSpeed.x) * Number(this.divideSpeed.x) + Number(this.divideSpeed.y) * Number(this.divideSpeed.y));
		}
		
		internal function move(param1:Point):void
		{
			this.x += param1.x;
			this.y += param1.y;
		}
	}
}
